#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

struct tree_node{
	char c;
	struct tree_node *left;
	struct tree_node *right;
};

//o(2n)
int compare_tree(struct tree_node* ta,struct tree_node* tb){
	int equal=0;
	if(ta==NULL && tb==NULL)return 1;
	else if( (ta==NULL && tb!=NULL) || (ta!=NULL && tb==NULL) )return 0;
	
	if(ta->c==tb->c){
		equal=compare_tree(ta->left,tb->left);
		if(equal==0)return 0;
		equal=compare_tree(ta->right,tb->right);
		if(equal==0)return 0;
		
		equal=compare_tree(ta->right,tb->left);
		if(equal==0)return 0;
		equal=compare_tree(ta->left,tb->right);
		if(equal==0)return 0;
		
	}else{
		return 0;
	}
	return 1;
}

//0x8140 - 0xFEFE
// has error ,assume 2 near by
int filter_ansi(char* gbk_string){
	int rc=0;
	char* pt=gbk_string;
	while(*pt!='\0'){
		char c=*pt;
		pt++;
		char d=*pt;
		int ch= (c<<8)|d;
		pt++;
		if(ch>=0x8140 && ch<=0xFEFE)		
			continue;
		if(isdigit(c) || isalpha(c)){
			*(pt-2)=' ';
			rc++;
		}
		if(isdigit(d) || isalpha(d)){
			*(pt-1)=' ';
			rc++;
		}
	}
	return rc;
}

void* mymemmove(void* dest,const void * src,size_t n){
	assert((dest!=0)&&(src!=0));
	char* d=(char*)dest;
	char* s=(char*)src;
	for(;n>0;n--){
		*d++=*s++;
	}
	return dest;
}

void revert(char* str){
	int len=strlen(str);
	char* f=str;
	char* e=str+len-1;
	while(f!=e){
		char t=*f;
		*f=*e;
		*e=t;		
		f++;
		e--;
	}
}
//0x8140 - 0xFEFE

int inplace_filter_ansi(char* gbk_string){
	int rc=0;
	char* pt=gbk_string;
	char* s=gbk_string;
	
	while(*pt!='\0'){
		char c=*pt;
		char d=*(pt+1);
		
		//printf("%x,%x\n",c<<8,d);
		
		int ch=  ((short)c & 0x00ff)<<8 | ((short)d & 0x00ff);
		//printf("%x ch\n",ch);
		if(ch >=0x8140 && ch<=0xFEFE){
			printf("is ch\n");
			*s=c;
			*(s+1)=d;
			s+=2;
			pt+=2;	
			continue;
		}
		if(isdigit(c) || isalpha(c)){
			//*(pt-2)=' ';
			pt++;
			rc++;
		}else{
			*s=*pt;
			pt++;
			s++;
			pt++;			
		}
	}
	*s='\0';
	return rc;
}

int main(){
	char ch[]="hello note this";
	char ch1[]="中文hell123测试abc\0";
	printf("ch: %s\n\n",ch);
	int rc=inplace_filter_ansi(ch);
	printf("ch new:%s ,%d, remove:%d\n",ch,strlen(ch),rc);
	
	printf("ch1: %s\n\n",ch1);
	rc=inplace_filter_ansi(ch1);
	printf("ch1 new:%s ,%d ,remove:%d\n",ch1,strlen(ch1),rc);
	
	char re[]=" you see this";
	revert(re);
	printf("%s\n",re);
	int x=sqrt(36);
	printf("%d\n",x);
	
	char xx='a'+('A'-'a');

	printf("%d %x",'A'-'a',xx & 0x80);
	printf("%d",'\0'==0);
}